package com.cloudpivot.receivales;

import com.cloudpivot.receivales.contract.domain.oa.OAContractItem;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class CloudpivotApplicationTest5 {

    @Test
    public void testmap() {
        List<OAContractItem> lists = new ArrayList<>();
        //准备数据
        OAContractItem item1=new OAContractItem();
        item1.setChanpinbm("SJ01002");
        OAContractItem item2=new OAContractItem();
        item2.setChanpinbm("SJ01002");
        OAContractItem item3=new OAContractItem();
        item3.setChanpinbm("SJ01003");
        OAContractItem item4=new OAContractItem();
        item4.setChanpinbm("SJ01004");
        OAContractItem item5=new OAContractItem();
        item5.setChanpinbm("SJ01004");
        OAContractItem item6=new OAContractItem();
        item6.setChanpinbm("SJ01002");
        lists.add(item1);
        lists.add(item2);
        lists.add(item3);
        lists.add(item4);
        lists.add(item5);
        lists.add(item6);

        Map<String, Integer> idMap = new HashMap<>();
        //生产序列字典
        for (OAContractItem item : lists) {
            if(idMap.containsKey(item.getChanpinbm())){
                //已经存在 ++
                String code=item.getChanpinbm();
                Integer id=idMap.get(code);
                id++;
                idMap.put(code, id);
            }else{
                //不存在
                String code=item.getChanpinbm();
                idMap.put(code, 0);
            }
            //消费序列字典
            String strCode = String.format("%s%s", item.getChanpinbm(), idMap.get(item.getChanpinbm())==0?"":idMap.get(item.getChanpinbm()));
            System.out.println(strCode);
        }

    }
}
